<?php

namespace Itwmw\Attributes\Validate;

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class PropertyValidator
{
    /**
     * @param class-string               $dataClass 数据类
     * @param array<string>|null         $fields    要验证的数据字段
     * @param string                     $scene     验证场景
     * @param array<EventFunc>|EventFunc $before    验证前执行的事件
     * @param array<EventFunc>|EventFunc $after     验证完毕后执行的事件
     */
    public function __construct(
        public readonly string $dataClass,
        public readonly ?array $fields = [],
        public readonly string $scene = '',
        public readonly array|EventFunc $before = [],
        public readonly array|EventFunc $after = [],
    ) {
        if (!class_exists($this->dataClass)) {
            throw new \InvalidArgumentException('没有找到指定的数据类');
        }
    }

    /**
     * @return array<EventFunc>
     */
    public function getAfter(): array
    {
        return is_array($this->after) ? $this->after : [$this->after];
    }

    /**
     * @return array<EventFunc>
     */
    public function getBefore(): array
    {
        return is_array($this->before) ? $this->before : [$this->before];
    }
}
